package com.imay.live.activity.family;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.imay.live.R;
import com.imay.live.activity.BaseRequestActivity;
import com.imay.live.activity.family.controller.FamilyFeedController;
import com.imay.live.activity.family.controller.FamilyHomeController;
import com.imay.live.fragment.dynamics.DynamicsListener;
import com.imay.live.fragment.dynamics.LinearFragment;
import com.imay.live.model.FeedModel;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/9/14 0014.
 */

public class FamilyFeedActivity extends BaseRequestActivity implements DynamicsListener {
    private String loadFamilyFeedCommand;

    private FamilyFeedController controller;

    private static final String KEY_FAMILY_ID = "key_family_id";

    private int familyId;

    private int start;
    private int count = 20;

    /**
     * @param context
     */
    public static void startActivity(Context context, int familyId) {
        Intent intent = new Intent(context, FamilyFeedActivity.class);
        intent.putExtra(KEY_FAMILY_ID, familyId);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_family_feed;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setTitle(R.string.str_family_dynamics);
        familyId = getIntent().getIntExtra(KEY_FAMILY_ID, -1);
        getIntent().putExtra(LinearFragment.FOLLOW, true);
        controller = (FamilyFeedController) FamilyFeedController.Builder.create(this, FamilyFeedController.class).build();

        loadFamilyFeed();
    }

    private void loadFamilyFeed() {
        if (familyId == -1) {
            return;
        }
        loadFamilyFeedCommand = ProtoRequestHelper.task(ProtoHttpFactory.getFamilyFeed(familyId, start, count, 0)).setTag(this.hashCode()).request(this);
    }

    @Override
    public void onRequestSuccess(String command, String data) {
        super.onRequestSuccess(command, data);
        if (command.equals(loadFamilyFeedCommand)) {
            Result<JSONObject> result = JSON.parseObject(data, new TypeReference<Result<JSONObject>>() {
            }.getType());
            if (result.data.containsKey("feeds")) {
                ArrayList<FeedModel> feedModels = JSON.parseObject(result.data.getString("feeds"), new TypeReference<ArrayList<FeedModel>>() {
                }.getType());
                if (start == 0) {
                    controller.setFeedData(feedModels);
                } else {
                    controller.addFeedData(feedModels);
                }
                start = start + feedModels.size();
            }
        }
    }

    @Override
    public void onRequestError(String command, int code, String message) {
        super.onRequestError(command, code, message);
        if (command.equals(loadFamilyFeedCommand)) {

        }
    }

    @Override
    public void scrollToPosition(int position) {

    }

    @Override
    public void loadMore() {
        loadFamilyFeed();
    }

    @Override
    public void refreshData() {
        start = 0;
        loadFamilyFeed();
    }
}
